﻿using System;
using System.Collections.Generic;
using System.Text;
using RoBOSSCommunicator;

namespace MazeRaider
{
    class MazeRaider
    {
        private Communicator communicator = new Communicator();
        private AlgorithmContainer container = new AlgorithmContainer();
        private Robot[] robots;

        public bool ConnectToController()
        {
            int result = communicator.Connect("192.168.0.11", "4468", "MazeRaider");
            if (result == 0)
            {
                RoBOSSCommunicator.Robot[] rawRobots = communicator.robots;
                robots = new Robot[rawRobots.Length];
                for (int i = 0; i < rawRobots.Length; i++)
                {
                    robots[i] = new Robot("Robot" + (i + 1), rawRobots[i].id);
                }
            }
            else
            {
                Console.WriteLine("Error while connecting to controller");
            }
            return result == 0;
        }

        public void RequestRobots()
        {
            for (int i = 0; i < robots.Length; i++)
            {
                robots[i].Request();
            }
        }

        public void SelectAlgorithm(AlgorithmEnum algorithmEnum)
        {
            container.SelectAlgorithm(algorithmEnum, robots);
        }

        public void Start()
        {
            communicator.StartSimulation();
            container.Start();
        }

        public void Stop()
        {
            container.Stop();
        }

        public void Reset()
        {
            communicator.StopSimulation();
            communicator.ResetSimulation();
        }

        public void ReleaseRobots()
        {
            for (int i = 0; i < robots.Length; i++)
            {
                robots[i].Release();
            }
        }

        public void DisconnectFromController()
        {
            communicator.Dispose();
        }
    }
}
